Passed
Push — master ( 152bc9...7b1700 )
by Miguel Ángel
02:01
created

Database.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 2
b 0
f 0
nc 1
dl 0
loc 9
rs 10
nop 2
1
const mysql = require('mysql')
2
3
class Database {
4
    constructor(config) {
5
        this.connection = mysql.createConnection(config);
6
    }
7
8
    query(sql, args) {
9
        return new Promise((resolve, reject) => {
10
            this.connection.query(sql, args, (error, rows) => {
11
                if (error) {
12
                    return reject(error)
13
                }
14
15
                return resolve(rows)
16
            })
17
        })
18
    }
19
20
    close() {
21
        return new Promise((resolve, reject) => {
22
            this.connection.end( err => {
0 ignored issues
show
Unused Code introduced by
The parameter err is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
23
                if (error) {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable error is declared in the current environment, consider using typeof error === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
24
                    return reject(error)
25
                }
26
27
                return resolve()
28
            })
29
        })
30
    }
31
}
32
33
Database.execute = function (config, callback) {
34
    const database = new Database(config)
35
36
    return callback(database).then(
37
        result => database.close().then(() => result),
38
        error => database.close().then(() => { throw error })
39
    )
40
}
41
42
module.exports = {
43
    Database
44
}
45